// -*- coding: utf-8; mode: groovy -*-
import org.apache.tools.ant.filters.*

buildscript {
	ext {
		springBootVersion = '1.3.5.RELEASE'
	}
	repositories {
		mavenCentral()
		maven { url "https://plugins.gradle.org/m2/" }
		maven { url 'http://repo.spring.io/plugins-release' }
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
		classpath "jp.classmethod.aws:gradle-aws-plugin:0.24"
		classpath("gradle.plugin.com.boxfuse.client:flyway-release:4.0.1")
		classpath("org.springframework.build.gradle:propdeps-plugin:0.0.7")
	}
}

version = currentVersion
group = "com.ari"
ext.artifactId = "axcis"
ext.defaultEncoding = "UTF-8"

if (hasProperty('env') == false) { ext.env = 'personal' }
apply from: "env/${env}/${env}.gradle"

apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: "idea"
apply plugin: "org.flywaydb.flyway"
apply plugin: "eclipse"

war {
	project.clean
	baseName = 'axcis'
	version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

configure(allprojects) {
	apply plugin: 'propdeps'
	apply plugin: 'propdeps-maven'
	apply plugin: 'propdeps-idea'
	apply plugin: 'propdeps-eclipse'
}

compileJava.dependsOn processResources {
	from('src/main/resources') {
		// 対象拡張子のみコピー
		include '**/*.yml'

		// トークンを置換する
		filter ReplaceTokens, tokens: [
				"DB_USERNAME": axcis.local.database.user,
				"DB_PASSWORD": axcis.local.database.password,
				"JDBC_CONNECTION_STRING": axcis.local.database.connectionString,
				"JDBC_TEST_CONNECTION_STRING": axcis.local.database.connectionTestString,
				"DEFAULT_PROFILE": axcis.local.springProfile
		]
	}

	// 残りのファイルをコピー
	from(sourceSets.main.resources.srcDirs) {
		exclude '**/*.yml'

	}
}

tasks.withType(AbstractCompile) each {
	it.options.encoding = ext.defaultEncoding
}

// ======== libraries ========
repositories {
	mavenCentral artifactUrls: [
			'http://jasperreports.sourceforge.net/maven2',
			'http://jaspersoft.artifactoryonline.com/jaspersoft/third-party-ce-artifacts/'
	]
	maven { url "https://repo.spring.io/snapshot" }
	maven { url "https://repo.spring.io/milestone" }
}

configurations {
	providedRuntime
}

configurations.all {
	resolutionStrategy.eachDependency { DependencyResolveDetails details ->
		if (details.requested.name == "commons-logging") {
			// prefer "jcl-over-slf4j" over "commons-logging":
			details.useTarget "org.slf4j:jcl-over-slf4j:$slf4jVersion"
		}
		if (details.requested.name == "log4j") {
			// prefer "log4j-over-slf4j" over "log4j":
			details.useTarget "org.slf4j:log4j-over-slf4j:$slf4jVersion"
		}
	}
}

dependencies {
	// Spring
	compile('org.springframework.boot:spring-boot-starter-cache')
	compile('org.springframework.boot:spring-boot-starter-data-jpa')
	compile('org.springframework.boot:spring-boot-devtools')
	compile('org.springframework.boot:spring-boot-starter-mail')
	compile('org.springframework.boot:spring-boot-starter-security')
	compile('org.springframework.session:spring-session')
	compile('org.springframework.boot:spring-boot-starter-thymeleaf')
	compile('org.springframework.boot:spring-boot-starter-web')
	compile "org.springframework.boot:spring-boot-starter-jdbc:$springBootVersion"
	compile ("org.springframework.boot:spring-boot-configuration-processor")

	// loggers
	compile "org.slf4j:slf4j-api"
	compile "org.slf4j:jul-to-slf4j"
	compile "ch.qos.logback:logback-classic"

	// Cloud
	// compile('org.springframework.cloud:spring-cloud-starter-aws')
	// compile('org.springframework.cloud:spring-cloud-starter-aws-jdbc')
	// compile('org.springframework.cloud:spring-cloud-starter-aws-messaging')

	// Other
	compile "mysql:mysql-connector-java:$mysqlVersion"
	providedCompile "org.projectlombok:lombok:$lombokVersion"
	compile "org.flywaydb:flyway-core:$flywayVersion"
	compile "javax.mail:mail:$javaMailVersion"
	compile('org.activiti:activiti-spring-boot-starter-basic:5.19.0')
	compile('com.fasterxml.jackson.core:jackson-core:2.7.4')
	compile('net.sf.jasperreports:jasperreports:6.2.2')
	compile('net.sf.supercsv:super-csv:2.3.1')
	compile('com.github.mygreen:super-csv-annotation:1.1')
	compile('net.sf.jett:jett-core:0.10.0')
	compile('net.sourceforge.nekohtml:nekohtml')
	compile('org.projectlombok:lombok:1.16.6')
	compile("com.googlecode.json-simple:json-simple:1.1")

	compile('org.springframework.boot:spring-boot-starter-tomcat')
	compile('org.springframework.boot:spring-boot-devtools')

	// testing
	testCompile 'org.assertj:assertj-core:3.4.1'
	testCompile('org.springframework.boot:spring-boot-starter-test')
	testCompile "junit:junit:$junitVersion"
	testCompile "org.hamcrest:hamcrest-library:$hamcrestVersion"
	testCompile "org.mockito:mockito-core:$mockitoCoreVersion"
	testCompile('org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16')
	compile 'org.flywaydb.flyway-test-extensions:flyway-spring4-test:4.0.1'
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.RELEASE"
	}
}

// ======== wrapper ========
task wrapper(type: Wrapper) {
	gradleVersion = "2.12"
}

tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
	project.getSystemProperties().each { key, value ->
		systemProperty key, value
	}
}

flyway {
	url = axcis.local.database.connectionString
	user = axcis.local.database.user
	password = axcis.local.database.password
	driver = 'com.mysql.jdbc.Driver'
}

Map<String, String> getSystemProperties() {
	def prop = [
			//		'JDBC_CONNECTION_STRING':  axcis.local.database.connectionString,
			//		'DB_USERNAME':             axcis.local.database.user,
			//		'DB_PASSWORD':             axcis.local.database.password,
			'spring.profiles.active':  axcis.local.springProfile,
	]
	if (file(axcis.local.logbackConfigurationFile).exists()) {
		prop.put('logging.config', axcis.local.logbackConfigurationFile)
	}
	return prop
}

String getAxcisVersion() {
	return uploadBundle.didWork ? uploadBundle.versionLabel : cloudFormation.getStackParameterValue("AxcisVersion")
}

eclipse {
	classpath {
		containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
		containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
	}
}
